package net.controller;

import net.pojo.*;
import net.service.OrderService;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.util.Calendar;
import java.util.List;

/**
 * @Author admin
 * @Date 2019/4/9 8:21
 */
@Controller
@RequestMapping("/order")
public class OrderController {
    @Autowired
    OrderService orderService;

    @RequestMapping("/findAll")
    public String findAll(Model model){
        List<Order> list = orderService.findAll();
        model.addAttribute("list",list);
        return "/admin/orders/list.jsp";
    }

    @RequestMapping("/findOrderDetails")
    public String findOrderDetails(String id ,Model model){
        Order orders = orderService.findByOrderId(id);
        model.addAttribute("order",orders);
        return "/admin/orders/view.jsp";
    }

    @RequestMapping("/searchOrder")
    @ResponseBody
    public List<Order> searchOrder(OrderVo orderVo){
        List<Order> list = orderService.search(orderVo);
        return list;
    }

    @RequestMapping("/download")
    public void download(DateVo dateVo, HttpServletResponse response)throws IOException{

        String fileName= "";
        try {
            if (StringUtils.isBlank(dateVo.getMonth())){
                fileName = URLEncoder.encode(dateVo.getYear()+"年销售排行","UTF-8");
            }else {
                fileName = URLEncoder.encode(dateVo.getYear()+"-"+dateVo.getMonth()+"月销售排行","UTF-8");
            }
        }catch (Exception e){
            fileName = "sale.xlsx";
        }

        List<Download> item = orderService.findItemByOid(dateVo);

        String title[] = {"商品号","商品名","总销量价"};

        Workbook wb = new HSSFWorkbook();
        /*创建个一个sheet*/
        Sheet sheet = wb.createSheet("统计");
        // 手动设置列宽。第一个参数表示要为第几列设；，第二个参数表示列的宽度，n为列高的像素数。
        for(int i=0;i<title.length;i++){
            sheet.setColumnWidth((short) i, (short) (35.7 * 150));
        }
        // 创建第一行
        int rowIndex = 0;
        Row row = sheet.createRow(rowIndex++);
        //设置列名
        for(int i=0;i<title.length;i++){
            Cell cell = row.createCell(i);
            cell.setCellValue(title[i]);
        }
        //同理可以设置数据行
            for(int i = 0;i<item.size();i++){
               row = sheet.createRow(rowIndex++);
               row.createCell(0).setCellValue(item.get(i).getPid());
               row.createCell(1).setCellValue(item.get(i).getName());
               row.createCell(2).setCellValue(item.get(i).getNumCount());

            }
        try {
            response.addHeader("Content-Disposition", "attachment;filename="+ new String((fileName + ".xls").getBytes(), "iso-8859-1"));
            wb.write(response.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }finally {

                wb.close();

        }
    }
    }

